package com.hgm.travelapp.fragment

import android.content.Intent
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.PopupWindow
import com.hgm.travelapp.R
import com.hgm.travelapp.databinding.FragmentPersonBinding
import java.io.File
import java.net.URI

class PersonFragment(private val home: HomeFragment) : Fragment(), View.OnClickListener {

    private val mBinding: FragmentPersonBinding by lazy {
        FragmentPersonBinding.inflate(layoutInflater)
    }
    private val nameDialog by lazy {
        NameDialog()
    }
    private val pwdDialog by lazy {
        PwdDialog()
    }
    private var popup: PopupWindow? = null


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return mBinding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        mBinding.apply {
            back.setOnClickListener(this@PersonFragment)
            changeHead.setOnClickListener(this@PersonFragment)
            changeName.setOnClickListener(this@PersonFragment)
            changePwd.setOnClickListener(this@PersonFragment)
        }
    }

    override fun onClick(v: View?) {
        val ft = activity?.supportFragmentManager?.beginTransaction()
        when (v?.id) {
            R.id.back -> ft?.replace(R.id.container, home)?.commit()
            R.id.changeHead -> initPopup()
            R.id.changeName -> nameDialog.show(childFragmentManager, "nameFragment")
            R.id.changePwd -> pwdDialog.show(childFragmentManager, "pwdFragment")
        }
    }


    private fun pickImageForPhoto() {
        //从相册里选择图片
        val intent = Intent(Intent.ACTION_PICK, null)
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*")
        startActivityForResult(intent, 1)
    }

    private fun pickImageForCamera() {
        //Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
        //      "head.jpg")));
        //startActivityForResult(intent2, 2);//采用ForResult打开
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        intent.putExtra(
            MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(File(Environment.getExternalStorageDirectory(), "head.jpg"))
        )
        startActivityForResult(intent, 2)
    }

    private fun initPopup() {
        //加载视图
        val view = LayoutInflater.from(activity).inflate(R.layout.popu_layout, null)
        //绑定控件
        val camera = view.findViewById<Button>(R.id.btn_camera)
        val select = view.findViewById<Button>(R.id.btn_select)
        val cancel = view.findViewById<Button>(R.id.btn_cancel)
        //设置宽高
        popup = PopupWindow(
            view,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        //配置PopupWindow
        popup?.apply {
            setBackgroundDrawable(BitmapDrawable())//设置背景颜色
            animationStyle = R.style.popup_anim_style//设置动画效果
            isFocusable = true
            isOutsideTouchable = true
            isTouchable = true//点击外部dismiss
            showAtLocation(activity?.window?.decorView, Gravity.BOTTOM, 0, 0)//设置显示位置
        }
        //点击事件
        camera.setOnClickListener {
            pickImageForCamera()
        }
        select.setOnClickListener {
            pickImageForPhoto()
        }
        cancel.setOnClickListener {
            popup?.dismiss()
        }
    }


}